package com.bytedance.scenedemo.ability_compatibility.theme;

import com.bytedance.scene.Scene;
import com.bytedance.scene.ktx.SceneExtensions;
import com.bytedance.scene.navigation.OnBackPressedListener;
import com.bytedance.scene.ui.template.AppCompatScene;
import com.bytedance.scenedemo.ResourceTable;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.colors.RgbPalette;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;

/**
 * @author JiangQi
 * @since 2018-09-11
 */
public class ThemeDemo extends AppCompatScene implements Component.ClickedListener,OnBackPressedListener {

    private Component parse;

    @Override
    protected Component onCreateContentView(LayoutScatter inflater, ComponentContainer container, Intent savedInstanceState) {
        parse = inflater.parse(ResourceTable.Layout_layout_theme, container, false);
        return parse;
    }

    @Override
    public void onAbilityCreated(Intent savedInstanceState) {
        super.onAbilityCreated(savedInstanceState);
        setTitle("主题");
        Text name = (Text) parse.findComponentById(ResourceTable.Id_name);
        name.setVisibility(Component.HIDE);
        Button btn = (Button) parse.findComponentById(ResourceTable.Id_btn);
        btn.setText("App 主题 1");
        btn.setClickedListener(this);

        Button btn2 = (Button) parse.findComponentById(ResourceTable.Id_btn2);
        btn2.setText("App 主题 2");
        btn2.setClickedListener(this);

        Button btn3 = (Button) parse.findComponentById(ResourceTable.Id_btn3);
        btn3.setText("App 主题 3");
        btn3.setClickedListener(this);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_btn:
                SceneExtensions.requireNavigationScene(this).push(TestTheme0Scene.class);
                requireAbility().getWindow().setStatusBarColor(Color.getIntColor("#FFBB32"));
                break;
            case ResourceTable.Id_btn2:
                SceneExtensions.requireNavigationScene(this).push(TestTheme1Scene.class);
                requireAbility().getWindow().setStatusBarColor(Color.getIntColor("#99CC00"));
                break;
            case ResourceTable.Id_btn3:
                SceneExtensions.requireNavigationScene(this).push(TestTheme2Scene.class);
                requireAbility().getWindow().setStatusBarColor(Color.getIntColor("#31B2E1"));
                break;
            default:
                break;
        }
    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
        requireAbility().getWindow().setStatusBarColor(Color.getIntColor("#404FB5"));
        requireAbility().getWindow().setNavigationBarColor(Color.getIntColor("#FEFEFE"));
    }

    @Override
    public boolean onBackPressed() {
        requireAbility().getWindow().setStatusBarColor(Color.getIntColor("#404FB5"));
        requireAbility().getWindow().setNavigationBarColor(Color.getIntColor("#FEFEFE"));
        return false;
    }

    public static class TestTheme0Scene extends Scene {

        @Override
        public Component onCreateView(LayoutScatter scatter, ComponentContainer container, Intent intent) {
            Component parse = scatter.parse(ResourceTable.Layout_layout_theme_demo, container, false);
            return parse;
        }

        @Override
        public void onAbilityCreated(Intent intent) {
            super.onAbilityCreated(intent);
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(RgbColor.fromArgbInt(RgbPalette.parse("#FFBB32")));
            this.getView().setBackground(shapeElement);
        }
    }

    public static class TestTheme1Scene extends Scene {

        @Override
        public Component onCreateView(LayoutScatter scatter, ComponentContainer container, Intent intent) {
            Component parse = scatter.parse(ResourceTable.Layout_layout_theme_demo, container, false);
            return parse;
        }

        @Override
        public void onAbilityCreated(Intent intent) {
            super.onAbilityCreated(intent);
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(RgbColor.fromArgbInt(RgbPalette.parse("#99CC00")));
            this.getView().setBackground(shapeElement);
        }
    }

    public static class TestTheme2Scene extends Scene {

        @Override
        public Component onCreateView(LayoutScatter scatter, ComponentContainer container, Intent intent) {
            Component parse = scatter.parse(ResourceTable.Layout_layout_theme_demo1, container, false);
            return parse;
        }

        @Override
        public void onAbilityCreated(Intent intent) {
            super.onAbilityCreated(intent);
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(RgbColor.fromArgbInt(RgbPalette.parse("#31B2E1")));
            this.getView().setBackground(shapeElement);
        }
    }
}
